/*****************************************
 * Copyright (c) 2008 by AvePoint, Inc
 *
 * AvePoint, Inc.
 * 3 Second Street, Suite 803
 * Jersey City, NJ 07311
 * United States of America
 * Telephone: +1-201-793-1111
 * WWW: www.avepoint.com
 */
package com.myassignment.client;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.myassignment.util.Constants;
import com.myassignment.util.DataPacketDto;

public class Client1 implements Constants {
    public static void main(String[] args) {
        try {
            ReceiveHandler receiver = new ReceiveHandler(Client_1_Port_Listen);
            Thread t = new Thread(receiver);
            t.start();

            DatagramSocket client = new DatagramSocket();
            // send registration
            DataPacketDto register = new DataPacketDto("Client1", "GateWay1");
            register.setTTL(9);
            register.setDelay(40);
            register.setSourceAdress("127.0.0.1");
            register.setTargetPort(Client_1_Port_Listen);
            register.setType(Request_type_Registration);

            String registerInfo = register.outerXML();
            DatagramPacket registerDataPack = new DatagramPacket(registerInfo.getBytes("UTF-8"),registerInfo.length(),
                    InetAddress.getByName("127.0.0.1"), Gate_Way_1_Port_Listen);
            client.send(registerDataPack);




            File file = new File("E:/Music/2.mp3");
            FileInputStream filein = new FileInputStream(file);
            DataInputStream in = new DataInputStream(filein);
            int size = 5 * 1024;
            byte[] buffer = new byte[size];
            int len = 0;
            System.out.println("Client1 begin send a file...");
            while((len = in.read(buffer)) != -1){
                System.out.println(len);
                DataPacketDto stamper = new DataPacketDto("Client1","Client2");
                stamper.setTTL(9);
                stamper.setType(Request_type_forward);
                stamper.setData(new String(buffer, 0, len));
                String sendInfomation = stamper.outerXML();
                DatagramPacket sendDataPack = new DatagramPacket(sendInfomation.getBytes("UTF-8"),sendInfomation.length(),
                        InetAddress.getByName("127.0.0.1"), Gate_Way_1_Port_Listen);
                client.send(sendDataPack);
            }
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class ReceiveHandler implements Runnable, Constants {
    private DatagramSocket datagramSocket;
    public ReceiveHandler(int port) {
        try {
            this.datagramSocket = new DatagramSocket(port);
        } catch (SocketException e) {
            e.printStackTrace();
        };
    }
    @Override
    public void run() {
        try {
            while(true) {
                byte[] buffer = new byte[1024*1024];
                DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
                datagramSocket.receive(datagramPacket);
                String receivedData = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
                System.out.println("Receive packet [IP:" + datagramPacket.getAddress().getHostAddress()+ ",Port:" + datagramPacket.getPort() + ", Data:" + receivedData + "]");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}